package com.sinosoft.weixin.controller;

import com.sinosoft.weixin.controller.vo.BaseInfoVO;
import com.sinosoft.weixin.controller.vo.GrpTypVO;
import com.sinosoft.weixin.controller.vo.OccupationVO;
import com.sinosoft.weixin.service.SysPubQrySerivce;
import org.apache.logging.log4j.LogManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by doney_mac on 2017/11/28.
 * 微信询价基本信息查询(业务来源,中介机构代码等)
 */
@RestController
@RequestMapping("api/commonQry")
public class BaseInfoQueryController {

     private static org.apache.logging.log4j.Logger logger = LogManager.getLogger(BaseInfoQueryController.class);

     public static final String BUSS_TYPE="child";

     public static final String OCCU_TYPE="occupationType";

     @Autowired
     private SysPubQrySerivce service;

     Map<String,Object> resultMap = new HashMap<>();


     public BaseInfoQueryController(){

     }

     /**
      * 请求查询业务来源,中介机构代码,企业性质
      * @return Object
      */
     @RequestMapping(value="baseInfoQry")
     public Object queryBaseInfo(){
          BaseInfoVO baseInfoVO = new BaseInfoVO();
          try {

               List resultList = new ArrayList();
               baseInfoVO.setBussSource(service.qryBussinessInfo());
               baseInfoVO.setGrpNature(service.qryGrpNature());
               baseInfoVO.setIntermediaryCode(service.qryIntermediaryCode());
               baseInfoVO.setRtnMssage("0", "交易成功!");
               return baseInfoVO;
          }catch (Exception e){
               logger.info(e.getMessage());
               baseInfoVO.setRtnMssage("1","交易失败,原因:"+e.getMessage());
               return baseInfoVO;
          }

     }

     /**
      * 请求查询所属行业大类 所属行业子类
      * @return
      */
     @RequestMapping(value="busstypeQry",produces ={ "application/json;charset=UTF-8" })
     @ResponseBody
     public Object queryBussType(){
          GrpTypVO grpTypVO=new GrpTypVO();
          //查询所属行业大类
          List<Map<String,Object>> list=service.qryGrpType(null);
          if(list!=null&&list.size()>0){
               for (Map m: list) {
                    String tCode= (String) m.get("id");
                    List<Map<String,Object>> lstBussType=service.qryBussType(tCode,null);
                    //logger.info(lstBussType);
                    m.put(BUSS_TYPE,lstBussType);
               }
          }else{
               grpTypVO.setRtnMssage("1","交易失败!:查询所属行业大类错误,请联系系统管理员!");
          }
          grpTypVO.setGrpType(list);
          grpTypVO.setRtnMssage("0","查询成功");
          return grpTypVO;

     }

     /**
      * 根据所属行业大类 所属行业小类 查询对应的职业类型
      * @return
      */
     @RequestMapping(value="occupationQry",method= RequestMethod.GET)
     public Object queryOccupation(@RequestParam(value="newGrpType",required = false) String tNewGrpType,@RequestParam(value="newBussType",required = false) String tNewBussType){
          OccupationVO occupationVO = new OccupationVO();

          try {
               logger.info(tNewGrpType);
               logger.info(tNewBussType);
               if (isNull(tNewGrpType) || isNull(tNewBussType)) {
                    occupationVO.setRtnMssage("1", "交易失败:原因请求参数不能为空!");
                    return occupationVO;
               }
               //查询所属行业大类信息
               List<Map<String, Object>> newGrpMapLst = service.qryGrpType(tNewGrpType);
               if(newGrpMapLst==null||newGrpMapLst.size()==0){
                    occupationVO.setRtnMssage("1","交易失败:所属行业大类编码:"+tNewGrpType+"不是有效编码,请联系系统管理员!");
                    return occupationVO;
               }
               //查询所属行业子类信息
               List<Map<String, Object>> newBussMapLst = service.qryBussType(null, tNewBussType);
               if(newBussMapLst==null||newBussMapLst.size()==0){
                    occupationVO.setRtnMssage("1","交易失败:所属行业小类编码:"+tNewBussType+"不是有效编码,请联系系统管理员!");
                    return occupationVO;
               }
               //根据行业子类查询职业信息
               List<Map<String, Object>> newOccupationLst = service.queryOccupation(tNewBussType);
               if(newOccupationLst==null||newOccupationLst.size()==0){
                    occupationVO.setRtnMssage("1","交易失败:查询职业编码失败,请联系系统管理员!");
                    return occupationVO;
               }
               //将职业信息至于map中
               newBussMapLst.get(0).put(OCCU_TYPE, newOccupationLst);
               //将子类信息放到大类信息map中
               newGrpMapLst.get(0).put(BUSS_TYPE, newBussMapLst);
               occupationVO.setRtnMssage("0", "交易成功");
               occupationVO.setOccupationQry(newGrpMapLst);
          }catch (Exception e){
               occupationVO.setRtnMssage("1", "交易失败:"+e.getMessage());
               return occupationVO;
          }
          return occupationVO;

     }

     /**
      * 判断非空
      * @return
      */
     public boolean isNull(String tParam){
          if(tParam ==null||"".equals(tParam)){
               return true;
          }
          return false;
     }
}
